package com.jse.upload;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.entity.Record;
import org.nutz.lang.Lang;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.upload.FastUploading;
import org.nutz.mvc.upload.Html5Uploading;
import org.nutz.mvc.upload.UploadAdaptor;
import org.nutz.mvc.upload.UploadException;
import org.nutz.mvc.upload.Uploading;
import org.nutz.mvc.upload.UploadingContext;
import org.nutz.mvc.upload.Uploads;

/**
 * 系统文件统一插件
 * 
 * @author dzh
 *
 */
public class JseUploadAdaptor extends UploadAdaptor {

	Log log = Logs.get();

	public JseUploadAdaptor() throws IOException {
		super();
	}

	public JseUploadAdaptor(UploadingContext uploadingContext) {
		super(uploadingContext);
	}

	@Override
	public Map<String, Object> getReferObject(ServletContext sc, HttpServletRequest request,
			HttpServletResponse response, String[] pathArgs) {
		try {
			if (!"POST".equals(request.getMethod()) && !"PUT".equals(request.getMethod())) {
				String str = "Not POST or PUT, Wrong HTTP method! --> " + request.getMethod();
				// throw Lang.makeThrow(IllegalArgumentException.class, str);
				log.info(str);
				Record map = new Record();
				request.getParameterMap().forEach((k, v) -> {
					map.put(k, v.length > 1 ? v : v[0]);
				});
				return map.toPojo(NutMap.class);
			}
			// 看看是不是传统的上传
			String contentType = request.getContentType();
			if (contentType == null) {
				Record map = new Record();
				request.getParameterMap().forEach((k, v) -> {
					map.put(k, v.length > 1 ? v : v[0]);
				});
				return map.toPojo(NutMap.class);
				// throw Lang.makeThrow(IllegalArgumentException.class, "Content-Type is
				// NULL!!");
			}
			if (contentType.contains("multipart/form-data")) { // 普通表单上传
				if (log.isDebugEnabled())
					log.debug("Select Html4 Form upload parser --> " + request.getRequestURI());
				Uploading ing = new FastUploading();
				return ing.parse(request, getContext());
			}
			if (contentType.contains("application/octet-stream")) { // Html5
																	// 流式上传
				if (log.isDebugEnabled())
					log.debug("Select Html5 Stream upload parser --> " + request.getRequestURI());
				Uploading ing = new Html5Uploading();
				return ing.parse(request, getContext());
			}
			// 100%是没写enctype='multipart/form-data'
			if (contentType.contains("application/x-www-form-urlencoded")) {
				log.warn("如果是要上传文件你的表单没有写--> enctype='multipart/form-data' ?");
				Record map = new Record();
				request.getParameterMap().forEach((k, v) -> {
					map.put(k, v.length > 1 ? Lang.list(v) : v[0]);
				});
				return map.toPojo(NutMap.class);
			}
			throw Lang.makeThrow(IllegalArgumentException.class, "Unknow Content-Type : " + contentType);
		} catch (UploadException e) {
			throw Lang.wrapThrow(e);
		} finally {
			Uploads.removeInfo(request);
		}
	}

}
